<?PHP
/**
 * 每小时执行任务处理类
 *
 * @author kamisama.xia@gmail.com 
 * @version 0.1
 */

class ome_autotask_timer_hour
{
    public function process($params, &$error_msg=''){
        set_time_limit(0);
        ignore_user_abort(1);

        //将发起库存同步1小时后，还是“同步中”的日志状态改为“失败”
        $db = kernel::database();
        $sql = "UPDATE sdb_ome_api_stock_log SET status='fail' WHERE status='running' AND createtime<".(time()-3600);
        $db->exec($sql);

        //将发起发货同步超过30分钟，状态还是“发货同步中”的订单改成“失败”
        $sql = "SELECT distinct orderBn, shopId FROM sdb_ome_shipment_log WHERE status = 'send' AND receiveTime < ".(time()-1800);
        $orderlist = $db->select($sql);
        if($orderlist){
            foreach($orderlist as $v){
                $order_arr[$v['shopId']][] = $v['orderBn'];
            }
            $shipmentLogObj = app::get('ome')->model('shipment_log');
            $orderObj = app::get('ome')->model('orders');
            foreach($order_arr as $k => $val){
                $orderObj->update(array('sync'=>'fail'),array('order_bn'=>$val,'shop_id'=>$k,'sync'=>'run'));
                $shipmentLogObj->update(array('status'=>'fail'),array('orderBn'=>$val,'shopId'=>$k,'status'=>'send'));
            }
        }

        //按小时/天的数据汇总
        kernel::single('eccommon_analysis_task')->analysis_hour();
        kernel::single('eccommon_analysis_task')->analysis_day();

        return true;
    }
}